package fucai.com.fucai.blmanager;

import android.content.Context;
import android.os.Handler;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


import fucai.com.fucai.R;
import fucai.com.fucai.bean.FucaiBean;
import fucai.com.fucai.bean.FucaiEntity;
import fucai.com.fucai.contains.AppConfig;
import fucai.com.fucai.db.DbHepler;
import fucai.com.fucai.db.FuCaiNumber;
import fucai.com.fucai.task.AddTxtToxUtilsThread;
import fucai.com.fucai.ui.NiuFuCai;
import fucai.com.fucai.utils.LinkUtils;

/**
 * Created by itte on 2015/12/12.
 */
public class NetDataManger extends BaseBLManager {
    private HttpUtils utils = null;

    /**
     * Initialize adapter
     *
     * @param context
     */
    public NetDataManger(Context context) {
        super(context);

    }

    /**
     * 根据数据库的期数请求数据
     *
     * @param handler
     * @param info
     * @return
     */
    public void getFromService(final Handler handler, int info) {
        utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET,
                AppConfig.getUrlNumber(info),
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String reslut = responseInfo.result;
                        String ss = reslut.replace("3D", "detail");
                        if (ss != null) {
                            FucaiEntity entity = JSON.parseObject(ss, FucaiEntity.class);
                            if (null != entity && entity.getCode_str().equals(mContext.getString(R.string.succeed))) {
                                changeData(entity.getInfo().getDetail());
                                handler.sendMessage(handler.obtainMessage(NiuFuCai.UPDATE_IS_OK_NET));
                            } else {
                                handler.sendMessage(handler.obtainMessage(NiuFuCai.UPDATE_IS_ERROR_NET));
                            }
                        } else {
                            handler.sendMessage(handler.obtainMessage(NiuFuCai.UPDATE_IS_NODATA_NET));
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        handler.sendMessage(handler.obtainMessage(NiuFuCai.UPDATE_IS_NOT_NET_NET));
                    }
                }
        );
    }

    /**
     * 把网络的数据转换为集合存入数据库
     *
     * @param list
     */
    public void changeData(List<FucaiBean> list) {
        mFuCaiNumbers = new ArrayList<>();
        for (FucaiBean fu : list) {
            FuCaiNumber fcn = new FuCaiNumber();
            fcn.setIssue(fu.getLotteryNo());
            String[] open = fu.getLotteryResult().split(";");
            int openBai = Integer.parseInt(open[0]);
            int openShi = Integer.parseInt(open[1]);
            int openGe = Integer.parseInt(open[2]);
            fcn.setOpenBaiWei(openBai);
            fcn.setOpenShiwei(openShi);
            fcn.setOpenGewei(openGe);
            fcn.setOpenSum(openBai + openGe + openShi);
            fcn.setOpenType(LinkUtils.numberType(openBai, openShi, openGe));
            fcn.setOpenSpan(LinkUtils.numberSpan(openBai, openShi, openGe));
            fcn.setOpenSumEnd((openBai + openShi + openGe) % 10);
            String[] test = fu.getSamplenum().split("");
            int testBai = Integer.parseInt(test[1]);
            int testShi = Integer.parseInt(test[2]);
            int testGe = Integer.parseInt(test[3]);
            fcn.setTestBaiWei(testBai);
            fcn.setTestShiwei(testShi);
            fcn.setTestGewei(testGe);
            fcn.setTestSum(testBai + testShi + testGe);
            fcn.setOpenSumEnd((testBai + testShi + testGe) % 10);
            fcn.setOpenType(LinkUtils.numberType(testBai, testShi, testGe));
            fcn.setTestSpan(LinkUtils.numberSpan(testBai, testShi, testGe));
            fcn.setWeek(fu.getWeek());
            fcn.setTime(fu.getYucePrizeTime());
            mFuCaiNumbers.add(fcn);
        }
        Collections.reverse(mFuCaiNumbers);
    }

    /**
     * 访问网络知道最新的数据
     *
     * @param
     */
    public void getNewDataForService(final Handler handler) {
        utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET,
                AppConfig.getUrlNumber(2),
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String result = responseInfo.result;
                        String ss = result.replace("3D", "detail");
                        if (ss != null) {
                            FucaiEntity entity = JSON.parseObject(ss, FucaiEntity.class);
                            if (null != entity && entity.getCode_str().equals(mContext.getString(R.string.succeed))) {
                                String netIssue = entity.getInfo().getDetail().get(0).getLotteryNo();
                                handler.sendMessage(handler.obtainMessage(NiuFuCai.UPDATE_IS_OK, netIssue));
                            } else {
                                handler.sendMessage(handler.obtainMessage(NiuFuCai.UPDATE_IS_ERROR));
                            }
                        } else {
                            handler.sendMessage(handler.obtainMessage(NiuFuCai.UPDATE_IS_NODATA));
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        handler.sendMessage(handler.obtainMessage(NiuFuCai.UPDATE_IS_NODATA));
                    }
                }
        );
    }

    /**
     * 判断是否是最新的 不是更新数据  传入网络的issue的值
     * 大于0不是需要更新
     *
     * @return
     */
    public int isUpdate(String netIssue) {
        String dbIssue = DbHepler.getInstance().findFrist();
        int updaetNumber = Integer.parseInt(netIssue) - Integer.parseInt(dbIssue);
        if (updaetNumber > 0) {
            return updaetNumber;
        } else {
            return 0;
        }
    }

    /**
     * 把网络的数据
     */
    public void addNetListToDb(Context context, Handler handler) {
        List<FuCaiNumber> li = mFuCaiNumbers;
        if (!li.isEmpty()) {
            new AddTxtToxUtilsThread(context, handler, li).start();
        } else {

        }
    }
}
